using Infrastructure.Attribute;
using Infrastructure.Extensions;
using JTZY.Model.Order.Dto;
using JTZY.Model.Order;
using JTZY.Repository;
using JTZY.Service.Order.IOrderService;
using JTZY.Model.Goods;

namespace JTZY.Service.Order
{
    /// <summary>
    /// 售后单明细表Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(ICorecmsbillaftersalesitemService), ServiceLifetime = LifeTime.Transient)]
    public class CorecmsbillaftersalesitemService : BaseService<Corecmsbillaftersalesitem>, ICorecmsbillaftersalesitemService
    {
        /// <summary>
        /// 查询售后单明细表列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<CorecmsbillaftersalesitemDto> GetList(CorecmsbillaftersalesitemQueryDto parm)
        {
            var predicate = QueryExp(parm);

            var response = Queryable()
                .Where(predicate.ToExpression())
                .ToPage<Corecmsbillaftersalesitem, CorecmsbillaftersalesitemDto>(parm);

            return response;
        }


        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="afterSalesId"></param>
        /// <returns></returns>
        public List<Corecmsbillaftersalesitem> GetInfo(string afterSalesId, long userId)
        {
            var response = Queryable()
                .LeftJoin<Corecmsuserstock>((it, us) => it.ProductId == us.ProductId && us.UserId == userId)
                .Where(it => it.AftersalesId == afterSalesId)
                .Select((it, us) => new Corecmsbillaftersalesitem
                {
                    AftersalesId = it.AftersalesId,
                    Addon = !string.IsNullOrEmpty(us.SkuName) ? us.SkuName : it.Addon,
                    ImageUrl = !string.IsNullOrEmpty(us.SkuImage) ? us.SkuImage : it.ImageUrl,
                    Nums = it.Nums,
                    Stock = us.Stock != null ? us.Stock.Value : 0,
                    ProductId = it.ProductId
                })
                .ToList();

            return response;
        }

        /// <summary>
        /// 添加售后单明细表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public Corecmsbillaftersalesitem AddCorecmsbillaftersalesitem(Corecmsbillaftersalesitem model)
        {
            return Insertable(model).ExecuteReturnEntity();
        }

        /// <summary>
        /// 修改售后单明细表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateCorecmsbillaftersalesitem(Corecmsbillaftersalesitem model)
        {
            return Update(model, true);
        }

        /// <summary>
        /// 查询导出表达式
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        private static Expressionable<Corecmsbillaftersalesitem> QueryExp(CorecmsbillaftersalesitemQueryDto parm)
        {
            var predicate = Expressionable.Create<Corecmsbillaftersalesitem>();

            return predicate;
        }
    }
}